{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "1.观察Otto商品的特征进行PCA各维的方差，可以得到什么结论？\n",
    "降维后47个主成分占了85%，说明47个成分就可以代替原始特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "2.对Otto商品tfidf特征，进行PCA降维，给出各维方差的分布图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.decomposition import PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>feat_1_tfidf</th>\n",
       "      <th>feat_2_tfidf</th>\n",
       "      <th>feat_3_tfidf</th>\n",
       "      <th>feat_4_tfidf</th>\n",
       "      <th>feat_5_tfidf</th>\n",
       "      <th>feat_6_tfidf</th>\n",
       "      <th>feat_7_tfidf</th>\n",
       "      <th>feat_8_tfidf</th>\n",
       "      <th>feat_9_tfidf</th>\n",
       "      <th>...</th>\n",
       "      <th>feat_85_tfidf</th>\n",
       "      <th>feat_86_tfidf</th>\n",
       "      <th>feat_87_tfidf</th>\n",
       "      <th>feat_88_tfidf</th>\n",
       "      <th>feat_89_tfidf</th>\n",
       "      <th>feat_90_tfidf</th>\n",
       "      <th>feat_91_tfidf</th>\n",
       "      <th>feat_92_tfidf</th>\n",
       "      <th>feat_93_tfidf</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.081393</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.075886</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.231403</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.199730</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>0.011987</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.011668</td>\n",
       "      <td>0.105971</td>\n",
       "      <td>0.021681</td>\n",
       "      <td>0.080435</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.008244</td>\n",
       "      <td>0.022456</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.124622</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.145988</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 95 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   id  feat_1_tfidf  feat_2_tfidf  feat_3_tfidf  feat_4_tfidf  feat_5_tfidf  \\\n",
       "0   1      0.081393           0.0           0.0      0.000000      0.000000   \n",
       "1   2      0.000000           0.0           0.0      0.000000      0.000000   \n",
       "2   3      0.000000           0.0           0.0      0.000000      0.000000   \n",
       "3   4      0.011987           0.0           0.0      0.011668      0.105971   \n",
       "4   5      0.000000           0.0           0.0      0.000000      0.000000   \n",
       "\n",
       "   feat_6_tfidf  feat_7_tfidf  feat_8_tfidf  feat_9_tfidf  ...  feat_85_tfidf  \\\n",
       "0      0.000000      0.000000      0.000000           0.0  ...       0.075886   \n",
       "1      0.000000      0.000000      0.231403           0.0  ...       0.000000   \n",
       "2      0.000000      0.000000      0.199730           0.0  ...       0.000000   \n",
       "3      0.021681      0.080435      0.000000           0.0  ...       0.000000   \n",
       "4      0.000000      0.000000      0.000000           0.0  ...       0.124622   \n",
       "\n",
       "   feat_86_tfidf  feat_87_tfidf  feat_88_tfidf  feat_89_tfidf  feat_90_tfidf  \\\n",
       "0       0.000000       0.000000            0.0            0.0       0.000000   \n",
       "1       0.000000       0.000000            0.0            0.0       0.000000   \n",
       "2       0.000000       0.000000            0.0            0.0       0.000000   \n",
       "3       0.008244       0.022456            0.0            0.0       0.000000   \n",
       "4       0.000000       0.000000            0.0            0.0       0.145988   \n",
       "\n",
       "   feat_91_tfidf  feat_92_tfidf  feat_93_tfidf   target  \n",
       "0            0.0            0.0            0.0  Class_1  \n",
       "1            0.0            0.0            0.0  Class_1  \n",
       "2            0.0            0.0            0.0  Class_1  \n",
       "3            0.0            0.0            0.0  Class_1  \n",
       "4            0.0            0.0            0.0  Class_1  \n",
       "\n",
       "[5 rows x 95 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.read_csv('train_tfidf.csv')\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_train = train['target']\n",
    "x_train = train.drop(['id','target'],axis =1)\n",
    "train_id = train['id']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ok\n"
     ]
    }
   ],
   "source": [
    "pca = PCA(n_components=0.85)\n",
    "pca.fit(x_train)\n",
    "x_train_pca = pca.transform(x_train)\n",
    "print('ok')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD4CAYAAADiry33AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAP4UlEQVR4nO3cf6zdd13H8efLlm0IcYOtGlyLLVk1lIAotSMRlTAdnUOKsYsdqDWZmSY0wQhiZ8KAislmDNXEmdjQah3qtsxfN664LAx/hODoHZMfZS5c6mTXLeyOluE0W+l4+8f5LpycnfZ+23tvb/s5z0dyc7/fz/fzPefzSXNfn08/53w/qSokSe36juVugCRpaRn0ktQ4g16SGmfQS1LjDHpJatzK5W7AqEsuuaTWrl273M2QpHPK/fff/0RVrRp37awL+rVr1zI9Pb3czZCkc0qS/zrRNZduJKlxBr0kNc6gl6TGGfSS1DiDXpIaZ9BLUuMMeklqnEEvSY0z6CWpcWfdk7ELtXbnXc8re/imq5ehJZJ0dug1o0+yOclDSWaS7Bxz/ceTfCbJ8SRbR65tT/Kl7mf7YjVcktTPvEGfZAVwC3AVsAG4NsmGkWpfAX4Z+MuRe18KvB+4HNgEvD/JSxbebElSX31m9JuAmao6XFXHgNuALcMVqurhqvoc8K2Re98M3FNVR6rqKHAPsHkR2i1J6qlP0F8KPDJ0PtuV9dHr3iTXJ5lOMj03N9fzpSVJffQJ+owpq56v3+veqtpTVRurauOqVWO3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FS0G/aXAI0Pns13ZpPmeqnoMBmEIfPcyt2fJdbum/hBwHxPU/27p4t+Bx4F7gC8DX6+q412Vlv8G/gB4L/Ct7vxiJqfvvbUY9L22XVBbkrwY+Gvg16vqG8vdnjOpqp6tqtcyePJ8E/DKcdXObKuWXpK3AI9X1f3DxWOqNtf3U9Vnr5tzjdsuDHw1ycuq6rEkL2Mw22tSkhcwCPm/qKq/6Yonpv/PqaqvJ/knBp9VXJRkZTezbfVv4EeBtyb5aeAC4LsYzPAnoe+npMUZfZ8tGybB8LYU24G/X8a2LJluTXYv8GBVfXjo0qT0f1WSi7rjFwI/yeBzik8w2I4EGu1/Vd1QVaurai2Dv/N7q+odTEDfT1WTT8Z2I/wf8O0tG353mZu0pJL8FfBGBtuzfhV4P/B3wB3Ay4GvANdU1egHtue8JG8A/hX4PN9ep/1tBuv0k9D/1zD4wHEFg4nbHVW1K8krGHwR4aXAA8AvVNUzy9fSpZXkjcB7quotk9b3PpoMeknSt7W4dCNJGmLQS1LjDHpJapxBL0mNM+glqXEGvSQ1zqCXpMb9PyA10MOwhWNfAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.bar(range(len(pca.explained_variance_ratio_)),pca.explained_variance_ratio_)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pca0</th>\n",
       "      <th>pca1</th>\n",
       "      <th>pca2</th>\n",
       "      <th>pca3</th>\n",
       "      <th>pca4</th>\n",
       "      <th>pca5</th>\n",
       "      <th>pca6</th>\n",
       "      <th>pca7</th>\n",
       "      <th>pca8</th>\n",
       "      <th>pca9</th>\n",
       "      <th>...</th>\n",
       "      <th>pca39</th>\n",
       "      <th>pca40</th>\n",
       "      <th>pca41</th>\n",
       "      <th>pca42</th>\n",
       "      <th>pca43</th>\n",
       "      <th>pca44</th>\n",
       "      <th>pca45</th>\n",
       "      <th>pca46</th>\n",
       "      <th>pca47</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>-0.230033</td>\n",
       "      <td>0.067735</td>\n",
       "      <td>-0.200102</td>\n",
       "      <td>-0.276508</td>\n",
       "      <td>-0.485227</td>\n",
       "      <td>0.391621</td>\n",
       "      <td>0.119618</td>\n",
       "      <td>-0.235460</td>\n",
       "      <td>-0.075811</td>\n",
       "      <td>-0.012476</td>\n",
       "      <td>...</td>\n",
       "      <td>0.048223</td>\n",
       "      <td>-0.078506</td>\n",
       "      <td>0.000444</td>\n",
       "      <td>0.088212</td>\n",
       "      <td>0.056207</td>\n",
       "      <td>0.050206</td>\n",
       "      <td>-0.018651</td>\n",
       "      <td>-0.012860</td>\n",
       "      <td>-0.033993</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>-0.165825</td>\n",
       "      <td>0.190047</td>\n",
       "      <td>-0.085411</td>\n",
       "      <td>-0.021620</td>\n",
       "      <td>-0.041540</td>\n",
       "      <td>-0.162239</td>\n",
       "      <td>0.014200</td>\n",
       "      <td>0.114020</td>\n",
       "      <td>-0.082616</td>\n",
       "      <td>-0.057659</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.122045</td>\n",
       "      <td>0.135781</td>\n",
       "      <td>-0.214742</td>\n",
       "      <td>0.065962</td>\n",
       "      <td>0.212053</td>\n",
       "      <td>-0.090437</td>\n",
       "      <td>-0.021293</td>\n",
       "      <td>-0.083192</td>\n",
       "      <td>0.211994</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>-0.040203</td>\n",
       "      <td>0.289302</td>\n",
       "      <td>-0.109865</td>\n",
       "      <td>-0.117562</td>\n",
       "      <td>-0.157621</td>\n",
       "      <td>-0.174803</td>\n",
       "      <td>-0.336212</td>\n",
       "      <td>-0.195427</td>\n",
       "      <td>0.160062</td>\n",
       "      <td>-0.106273</td>\n",
       "      <td>...</td>\n",
       "      <td>0.006395</td>\n",
       "      <td>0.101072</td>\n",
       "      <td>-0.057329</td>\n",
       "      <td>-0.125129</td>\n",
       "      <td>0.127905</td>\n",
       "      <td>0.019454</td>\n",
       "      <td>0.024437</td>\n",
       "      <td>-0.070221</td>\n",
       "      <td>0.075631</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>-0.121822</td>\n",
       "      <td>0.068306</td>\n",
       "      <td>-0.024844</td>\n",
       "      <td>-0.031293</td>\n",
       "      <td>0.013067</td>\n",
       "      <td>-0.112368</td>\n",
       "      <td>0.049243</td>\n",
       "      <td>0.034076</td>\n",
       "      <td>-0.067092</td>\n",
       "      <td>0.157353</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.005219</td>\n",
       "      <td>0.005743</td>\n",
       "      <td>0.002918</td>\n",
       "      <td>-0.031549</td>\n",
       "      <td>0.011688</td>\n",
       "      <td>-0.003040</td>\n",
       "      <td>-0.002959</td>\n",
       "      <td>-0.007025</td>\n",
       "      <td>-0.003449</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>-0.127224</td>\n",
       "      <td>0.126973</td>\n",
       "      <td>-0.016796</td>\n",
       "      <td>-0.134441</td>\n",
       "      <td>-0.063135</td>\n",
       "      <td>-0.185726</td>\n",
       "      <td>0.027807</td>\n",
       "      <td>0.099285</td>\n",
       "      <td>0.045462</td>\n",
       "      <td>0.037166</td>\n",
       "      <td>...</td>\n",
       "      <td>0.167076</td>\n",
       "      <td>0.116967</td>\n",
       "      <td>0.077692</td>\n",
       "      <td>0.234487</td>\n",
       "      <td>0.272050</td>\n",
       "      <td>0.069280</td>\n",
       "      <td>0.284079</td>\n",
       "      <td>0.275134</td>\n",
       "      <td>-0.066717</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 49 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       pca0      pca1      pca2      pca3      pca4      pca5      pca6  \\\n",
       "0 -0.230033  0.067735 -0.200102 -0.276508 -0.485227  0.391621  0.119618   \n",
       "1 -0.165825  0.190047 -0.085411 -0.021620 -0.041540 -0.162239  0.014200   \n",
       "2 -0.040203  0.289302 -0.109865 -0.117562 -0.157621 -0.174803 -0.336212   \n",
       "3 -0.121822  0.068306 -0.024844 -0.031293  0.013067 -0.112368  0.049243   \n",
       "4 -0.127224  0.126973 -0.016796 -0.134441 -0.063135 -0.185726  0.027807   \n",
       "\n",
       "       pca7      pca8      pca9  ...     pca39     pca40     pca41     pca42  \\\n",
       "0 -0.235460 -0.075811 -0.012476  ...  0.048223 -0.078506  0.000444  0.088212   \n",
       "1  0.114020 -0.082616 -0.057659  ... -0.122045  0.135781 -0.214742  0.065962   \n",
       "2 -0.195427  0.160062 -0.106273  ...  0.006395  0.101072 -0.057329 -0.125129   \n",
       "3  0.034076 -0.067092  0.157353  ... -0.005219  0.005743  0.002918 -0.031549   \n",
       "4  0.099285  0.045462  0.037166  ...  0.167076  0.116967  0.077692  0.234487   \n",
       "\n",
       "      pca43     pca44     pca45     pca46     pca47   target  \n",
       "0  0.056207  0.050206 -0.018651 -0.012860 -0.033993  Class_1  \n",
       "1  0.212053 -0.090437 -0.021293 -0.083192  0.211994  Class_1  \n",
       "2  0.127905  0.019454  0.024437 -0.070221  0.075631  Class_1  \n",
       "3  0.011688 -0.003040 -0.002959 -0.007025 -0.003449  Class_1  \n",
       "4  0.272050  0.069280  0.284079  0.275134 -0.066717  Class_1  \n",
       "\n",
       "[5 rows x 49 columns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n_components = pca.n_components_\n",
    "feat_names_pca = []\n",
    "for i in range(n_components):\n",
    "    feat_names_pca.append('pca'+str(i))\n",
    "y = pd.Series(data = y_train,name='target')\n",
    "train_pca = pd.concat([pd.DataFrame(columns = feat_names_pca,data=x_train_pca),y],axis=1)\n",
    "train_pca.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "3.采用train_test_split，从将数据集中随机抽取10000条记录（原始数据集太大，剩余数据抛弃，此部分SVM作业已经完成）。\n",
    "对这部分数据进行PCA降维，保留85%的能量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10000, 93)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "49661    Class_8\n",
       "50982    Class_8\n",
       "35173    Class_6\n",
       "24156    Class_3\n",
       "20955    Class_3\n",
       "          ...   \n",
       "22033    Class_3\n",
       "2947     Class_2\n",
       "3845     Class_2\n",
       "61854    Class_9\n",
       "50280    Class_8\n",
       "Name: target, Length: 10000, dtype: object"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "x_train_part,x_val,y_train_part,y_val = train_test_split(x_train,y_train,train_size = 10000)\n",
    "print(x_train_part.shape)\n",
    "y_train_part"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ok\n"
     ]
    }
   ],
   "source": [
    "pca1 = PCA(n_components=0.85)\n",
    "pca1.fit(x_train_part)\n",
    "x_train_pca1 = pca1.transform(x_train_part)\n",
    "print('ok')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD4CAYAAADiry33AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAP1klEQVR4nO3df6zd9V3H8efLdsDcImxQzaTM24Vq1mVzau2WOHUBx4rMdcaSlE2tCQZN1mTGLbOYjLE6k2HMuj+GiY2gBIxA8NeNdBIy5o8sk/Uy3FjBZneI4woZF9sxMQFW9vaP8yU7OT3t/V56bi/9nOcjae73+/l+vud8PiH39fnwOef7uakqJEnt+r7VboAkaWUZ9JLUOINekhpn0EtS4wx6SWrc2tVuwKjzzjuvZmZmVrsZknRaue+++56sqnXjrr3kgn5mZoa5ubnVboYknVaS/Nfxrrl0I0mNM+glqXEGvSQ1zqCXpMYZ9JLUOINekhpn0EtS4wx6SWpcr6BPsjXJoSTzSXaPuf5zSb6U5GiS7SPXdib5Wvdv56QaLknqZ8knY5OsAa4H3gEsAAeSzFbVg0PVvgH8BvChkXtfDXwU2AwUcF9375HJNP9YM7vvPKbskU9ctlJvJ0kveX1m9FuA+ap6uKqeA24Ftg1XqKpHquorwHdH7n0ncHdVHe7C/W5g6wTaLUnqqU/Qnw88OnS+0JX10eveJFclmUsyt7i42POlJUl99An6jCnr+4dme91bVfuqanNVbV63buzma5KkF6lP0C8AFwydrwce6/n6J3OvJGkC+gT9AWBjkg1JzgB2ALM9X/8u4JIkr0ryKuCSrkySdIosGfRVdRT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9X1VvZvDk+Rbg9eOqndpWrbwk7wKeqKr7hovHVG2u78vVZ6+b043bLgx8M8lrqurxJK9hMNtrUpKXMQj5v6yqv+mKp6b/L6iqbyX5JwafVZyTZG03s231d+BngHcn+UXgLOAHGMzwp6Hvy9LijL7Plg3TYHhbip3A369iW1ZMtyZ7A/BQVX1y6NK09H9dknO645cDv8Dgc4rPMdiOBBrtf1VdXVXrq2qGwe/5PVX1Pqag78vV5JOx3Qj/Kb63ZcMfrnKTVlSSvwLezmB71m8CHwX+DrgdeC3wDeDyqhr9wPa0l+RtwL8CD/C9ddrfZ7BOPw39fxODDxzXMJi43V5Ve5K8jsEXEV4N3A/8alU9u3otXVlJ3g58qKreNW1976PJoJckfU+LSzeSpCEGvSQ1zqCXpMYZ9JLUOINekhpn0EtS4wx6SWrc/wM0g9EZGLfHRgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.bar(range(len(pca1.explained_variance_ratio_)),pca1.explained_variance_ratio_)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pca0</th>\n",
       "      <th>pca1</th>\n",
       "      <th>pca2</th>\n",
       "      <th>pca3</th>\n",
       "      <th>pca4</th>\n",
       "      <th>pca5</th>\n",
       "      <th>pca6</th>\n",
       "      <th>pca7</th>\n",
       "      <th>pca8</th>\n",
       "      <th>pca9</th>\n",
       "      <th>...</th>\n",
       "      <th>pca39</th>\n",
       "      <th>pca40</th>\n",
       "      <th>pca41</th>\n",
       "      <th>pca42</th>\n",
       "      <th>pca43</th>\n",
       "      <th>pca44</th>\n",
       "      <th>pca45</th>\n",
       "      <th>pca46</th>\n",
       "      <th>pca47</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>-0.218228</td>\n",
       "      <td>-0.158228</td>\n",
       "      <td>-0.069056</td>\n",
       "      <td>0.066624</td>\n",
       "      <td>-0.150943</td>\n",
       "      <td>-0.326850</td>\n",
       "      <td>-0.074052</td>\n",
       "      <td>0.246960</td>\n",
       "      <td>-0.057051</td>\n",
       "      <td>-0.279175</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.187159</td>\n",
       "      <td>0.063349</td>\n",
       "      <td>-0.087299</td>\n",
       "      <td>-0.060649</td>\n",
       "      <td>-0.115997</td>\n",
       "      <td>0.097422</td>\n",
       "      <td>0.008557</td>\n",
       "      <td>-0.083032</td>\n",
       "      <td>0.003544</td>\n",
       "      <td>Class_8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>-0.244994</td>\n",
       "      <td>-0.004169</td>\n",
       "      <td>-0.059383</td>\n",
       "      <td>0.040338</td>\n",
       "      <td>-0.071929</td>\n",
       "      <td>-0.288672</td>\n",
       "      <td>0.016334</td>\n",
       "      <td>0.282802</td>\n",
       "      <td>-0.160172</td>\n",
       "      <td>-0.209755</td>\n",
       "      <td>...</td>\n",
       "      <td>0.096258</td>\n",
       "      <td>-0.124141</td>\n",
       "      <td>-0.104618</td>\n",
       "      <td>-0.023857</td>\n",
       "      <td>0.020423</td>\n",
       "      <td>-0.030197</td>\n",
       "      <td>-0.101313</td>\n",
       "      <td>0.004387</td>\n",
       "      <td>-0.015110</td>\n",
       "      <td>Class_8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>-0.423970</td>\n",
       "      <td>0.387655</td>\n",
       "      <td>0.039661</td>\n",
       "      <td>-0.108337</td>\n",
       "      <td>0.042702</td>\n",
       "      <td>-0.048267</td>\n",
       "      <td>-0.063967</td>\n",
       "      <td>0.004719</td>\n",
       "      <td>0.014013</td>\n",
       "      <td>-0.074279</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.004983</td>\n",
       "      <td>0.008251</td>\n",
       "      <td>-0.066105</td>\n",
       "      <td>0.008782</td>\n",
       "      <td>0.124509</td>\n",
       "      <td>0.093363</td>\n",
       "      <td>0.028075</td>\n",
       "      <td>0.025003</td>\n",
       "      <td>0.042902</td>\n",
       "      <td>Class_6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>-0.036398</td>\n",
       "      <td>-0.101075</td>\n",
       "      <td>0.737897</td>\n",
       "      <td>0.015869</td>\n",
       "      <td>0.005933</td>\n",
       "      <td>-0.075785</td>\n",
       "      <td>-0.032469</td>\n",
       "      <td>-0.200989</td>\n",
       "      <td>-0.220219</td>\n",
       "      <td>0.001071</td>\n",
       "      <td>...</td>\n",
       "      <td>0.024900</td>\n",
       "      <td>-0.008045</td>\n",
       "      <td>0.251055</td>\n",
       "      <td>-0.041540</td>\n",
       "      <td>-0.069615</td>\n",
       "      <td>0.066868</td>\n",
       "      <td>0.019241</td>\n",
       "      <td>-0.038727</td>\n",
       "      <td>-0.078828</td>\n",
       "      <td>Class_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>0.613382</td>\n",
       "      <td>0.175337</td>\n",
       "      <td>-0.241542</td>\n",
       "      <td>-0.078716</td>\n",
       "      <td>-0.008539</td>\n",
       "      <td>-0.001749</td>\n",
       "      <td>0.109710</td>\n",
       "      <td>-0.169055</td>\n",
       "      <td>-0.045808</td>\n",
       "      <td>-0.071331</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.011932</td>\n",
       "      <td>-0.041655</td>\n",
       "      <td>-0.029515</td>\n",
       "      <td>-0.045650</td>\n",
       "      <td>-0.061415</td>\n",
       "      <td>-0.018497</td>\n",
       "      <td>-0.037826</td>\n",
       "      <td>-0.053354</td>\n",
       "      <td>0.079077</td>\n",
       "      <td>Class_3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 49 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       pca0      pca1      pca2      pca3      pca4      pca5      pca6  \\\n",
       "0 -0.218228 -0.158228 -0.069056  0.066624 -0.150943 -0.326850 -0.074052   \n",
       "1 -0.244994 -0.004169 -0.059383  0.040338 -0.071929 -0.288672  0.016334   \n",
       "2 -0.423970  0.387655  0.039661 -0.108337  0.042702 -0.048267 -0.063967   \n",
       "3 -0.036398 -0.101075  0.737897  0.015869  0.005933 -0.075785 -0.032469   \n",
       "4  0.613382  0.175337 -0.241542 -0.078716 -0.008539 -0.001749  0.109710   \n",
       "\n",
       "       pca7      pca8      pca9  ...     pca39     pca40     pca41     pca42  \\\n",
       "0  0.246960 -0.057051 -0.279175  ... -0.187159  0.063349 -0.087299 -0.060649   \n",
       "1  0.282802 -0.160172 -0.209755  ...  0.096258 -0.124141 -0.104618 -0.023857   \n",
       "2  0.004719  0.014013 -0.074279  ... -0.004983  0.008251 -0.066105  0.008782   \n",
       "3 -0.200989 -0.220219  0.001071  ...  0.024900 -0.008045  0.251055 -0.041540   \n",
       "4 -0.169055 -0.045808 -0.071331  ... -0.011932 -0.041655 -0.029515 -0.045650   \n",
       "\n",
       "      pca43     pca44     pca45     pca46     pca47   target  \n",
       "0 -0.115997  0.097422  0.008557 -0.083032  0.003544  Class_8  \n",
       "1  0.020423 -0.030197 -0.101313  0.004387 -0.015110  Class_8  \n",
       "2  0.124509  0.093363  0.028075  0.025003  0.042902  Class_6  \n",
       "3 -0.069615  0.066868  0.019241 -0.038727 -0.078828  Class_3  \n",
       "4 -0.061415 -0.018497 -0.037826 -0.053354  0.079077  Class_3  \n",
       "\n",
       "[5 rows x 49 columns]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n_components = pca.n_components_\n",
    "feat_names_pca = []\n",
    "for i in range(n_components):\n",
    "    feat_names_pca.append('pca'+str(i))\n",
    "y = pd.Series(data = y_train_part,name='target')\n",
    "y = y.reset_index(drop=True)\n",
    "train_pca = pd.concat([pd.DataFrame(columns = feat_names_pca,data=x_train_pca1),y],axis=1)\n",
    "train_pca.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "4. 对3中得到的数据（对降维后的数据），训练RBF核SVM，并对超参数（C和gamma）进行超参数调优。结果和用原始数据的情况比较（SVM部分作业结果）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_train_part = train_pca['target']\n",
    "x_train_part = train_pca.drop(['target'],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C: 0.1, gamma: 0.1, accuracy: 0.704197571540392\n",
      "C: 0.1, gamma: 1.0, accuracy: 0.7314985349301862\n",
      "C: 0.1, gamma: 10.0, accuracy: 0.5065934109487421\n",
      "C: 1.0, gamma: 0.1, accuracy: 0.737897502359132\n",
      "C: 1.0, gamma: 1.0, accuracy: 0.7621982810271342\n",
      "C: 1.0, gamma: 10.0, accuracy: 0.7121010694543547\n",
      "C: 10.0, gamma: 0.1, accuracy: 0.7533974208561619\n",
      "C: 10.0, gamma: 1.0, accuracy: 0.7759968919590808\n",
      "C: 10.0, gamma: 10.0, accuracy: 0.7192011571472229\n",
      "C: 100.0, gamma: 0.1, accuracy: 0.770699470079763\n",
      "C: 100.0, gamma: 1.0, accuracy: 0.7574009765269402\n",
      "C: 100.0, gamma: 10.0, accuracy: 0.7167021039444105\n",
      "C: 1000.0, gamma: 0.1, accuracy: 0.7598009794545085\n",
      "C: 1000.0, gamma: 1.0, accuracy: 0.7482963034840454\n",
      "C: 1000.0, gamma: 10.0, accuracy: 0.7164023142778498\n"
     ]
    }
   ],
   "source": [
    "from mpl_toolkits import mplot3d\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.model_selection import cross_val_score\n",
    "C_s = np.logspace(-1,3,5)\n",
    "gamma_s = np.logspace(-1,1,3)\n",
    "accuracy_rbf = []\n",
    "for C in C_s:\n",
    "    for gamma in gamma_s:\n",
    "        svc_rbf = SVC(C=C,kernel='rbf',gamma=gamma)\n",
    "        accuracy = cross_val_score(svc_rbf,x_train_part,y_train_part,cv = 3,scoring='accuracy')\n",
    "        accuracy_rbf.append(accuracy.mean())\n",
    "        print('C: {}, gamma: {}, accuracy: {}'.format(C,gamma,accuracy.mean()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ok\n"
     ]
    }
   ],
   "source": [
    "print('ok')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "best c: 10.0, best gamma: 1.0\n"
     ]
    }
   ],
   "source": [
    "C_new = []\n",
    "gamma_new = []\n",
    "for i in C_s:\n",
    "    for j in gamma_s:\n",
    "        C_new.append(i)\n",
    "        gamma_new.append(j)\n",
    "index = np.argmax(accuracy_rbf,axis=None)\n",
    "Best_C = C_new[index]\n",
    "Best_gamma = gamma_new[index]\n",
    "print('best c: {}, best gamma: {}'.format(Best_C,Best_gamma))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SVC(C=10.0, cache_size=200, class_weight=None, coef0=0.0,\n",
       "    decision_function_shape='ovr', degree=3, gamma=1.0, kernel='rbf',\n",
       "    max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
       "    tol=0.001, verbose=False)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "svc = SVC(C = Best_C,gamma=Best_gamma,kernel='rbf',probability=False)\n",
    "svc.fit(x_train_part,y_train_part)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_test_pca = pca1.transform(x_val)\n",
    "n_components = pca1.n_components_\n",
    "feat_names_pca = []\n",
    "for i in range(n_components):\n",
    "    feat_names_pca.append('pca'+str(i))\n",
    "y = pd.Series(data = y_val,name='target')\n",
    "y = y.reset_index(drop=True)\n",
    "x_pca = pd.DataFrame(columns = feat_names_pca,data=x_test_pca)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        Class_8\n",
       "1        Class_5\n",
       "2        Class_2\n",
       "3        Class_3\n",
       "4        Class_5\n",
       "          ...   \n",
       "51873    Class_3\n",
       "51874    Class_9\n",
       "51875    Class_3\n",
       "51876    Class_6\n",
       "51877    Class_2\n",
       "Name: target, Length: 51878, dtype: object"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pca0</th>\n",
       "      <th>pca1</th>\n",
       "      <th>pca2</th>\n",
       "      <th>pca3</th>\n",
       "      <th>pca4</th>\n",
       "      <th>pca5</th>\n",
       "      <th>pca6</th>\n",
       "      <th>pca7</th>\n",
       "      <th>pca8</th>\n",
       "      <th>pca9</th>\n",
       "      <th>...</th>\n",
       "      <th>pca38</th>\n",
       "      <th>pca39</th>\n",
       "      <th>pca40</th>\n",
       "      <th>pca41</th>\n",
       "      <th>pca42</th>\n",
       "      <th>pca43</th>\n",
       "      <th>pca44</th>\n",
       "      <th>pca45</th>\n",
       "      <th>pca46</th>\n",
       "      <th>pca47</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>-0.043350</td>\n",
       "      <td>-0.060584</td>\n",
       "      <td>0.272129</td>\n",
       "      <td>-0.060398</td>\n",
       "      <td>-0.078276</td>\n",
       "      <td>-0.008805</td>\n",
       "      <td>-0.001642</td>\n",
       "      <td>0.349955</td>\n",
       "      <td>0.255530</td>\n",
       "      <td>-0.122431</td>\n",
       "      <td>...</td>\n",
       "      <td>0.085597</td>\n",
       "      <td>-0.063296</td>\n",
       "      <td>0.158532</td>\n",
       "      <td>0.040823</td>\n",
       "      <td>0.043348</td>\n",
       "      <td>0.065940</td>\n",
       "      <td>-0.111555</td>\n",
       "      <td>-0.075880</td>\n",
       "      <td>0.000997</td>\n",
       "      <td>-0.060340</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>-0.115760</td>\n",
       "      <td>-0.197918</td>\n",
       "      <td>-0.049863</td>\n",
       "      <td>0.666364</td>\n",
       "      <td>0.668454</td>\n",
       "      <td>0.086413</td>\n",
       "      <td>0.169695</td>\n",
       "      <td>-0.048810</td>\n",
       "      <td>0.026368</td>\n",
       "      <td>-0.005191</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.008948</td>\n",
       "      <td>-0.021257</td>\n",
       "      <td>-0.033957</td>\n",
       "      <td>-0.000682</td>\n",
       "      <td>0.032265</td>\n",
       "      <td>-0.014482</td>\n",
       "      <td>-0.006587</td>\n",
       "      <td>-0.005308</td>\n",
       "      <td>-0.027790</td>\n",
       "      <td>0.009790</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>0.021702</td>\n",
       "      <td>-0.164828</td>\n",
       "      <td>0.706077</td>\n",
       "      <td>0.023347</td>\n",
       "      <td>-0.080063</td>\n",
       "      <td>-0.018041</td>\n",
       "      <td>-0.056035</td>\n",
       "      <td>-0.278979</td>\n",
       "      <td>-0.177411</td>\n",
       "      <td>-0.068560</td>\n",
       "      <td>...</td>\n",
       "      <td>0.007054</td>\n",
       "      <td>-0.014735</td>\n",
       "      <td>0.021255</td>\n",
       "      <td>0.013600</td>\n",
       "      <td>-0.000227</td>\n",
       "      <td>0.081414</td>\n",
       "      <td>0.019109</td>\n",
       "      <td>0.026369</td>\n",
       "      <td>0.028131</td>\n",
       "      <td>-0.028596</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>0.063711</td>\n",
       "      <td>0.363892</td>\n",
       "      <td>0.147542</td>\n",
       "      <td>0.057119</td>\n",
       "      <td>-0.103717</td>\n",
       "      <td>0.282836</td>\n",
       "      <td>-0.073967</td>\n",
       "      <td>0.528988</td>\n",
       "      <td>0.261934</td>\n",
       "      <td>0.136618</td>\n",
       "      <td>...</td>\n",
       "      <td>0.162702</td>\n",
       "      <td>0.052842</td>\n",
       "      <td>0.071982</td>\n",
       "      <td>-0.149263</td>\n",
       "      <td>0.084534</td>\n",
       "      <td>0.043217</td>\n",
       "      <td>0.091920</td>\n",
       "      <td>-0.004109</td>\n",
       "      <td>-0.041666</td>\n",
       "      <td>0.076827</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>-0.081707</td>\n",
       "      <td>-0.242624</td>\n",
       "      <td>0.013579</td>\n",
       "      <td>0.528990</td>\n",
       "      <td>0.688504</td>\n",
       "      <td>0.188602</td>\n",
       "      <td>0.163560</td>\n",
       "      <td>0.034647</td>\n",
       "      <td>0.015191</td>\n",
       "      <td>0.002458</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.024562</td>\n",
       "      <td>-0.034539</td>\n",
       "      <td>-0.035536</td>\n",
       "      <td>-0.011611</td>\n",
       "      <td>0.032814</td>\n",
       "      <td>-0.022945</td>\n",
       "      <td>0.006963</td>\n",
       "      <td>0.003239</td>\n",
       "      <td>-0.014328</td>\n",
       "      <td>-0.005327</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>51873</td>\n",
       "      <td>0.000284</td>\n",
       "      <td>-0.068464</td>\n",
       "      <td>0.481083</td>\n",
       "      <td>0.027953</td>\n",
       "      <td>-0.031293</td>\n",
       "      <td>-0.210537</td>\n",
       "      <td>0.100820</td>\n",
       "      <td>-0.035262</td>\n",
       "      <td>-0.019532</td>\n",
       "      <td>0.049997</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.020131</td>\n",
       "      <td>-0.074710</td>\n",
       "      <td>0.101019</td>\n",
       "      <td>-0.043210</td>\n",
       "      <td>0.068722</td>\n",
       "      <td>-0.001956</td>\n",
       "      <td>0.050221</td>\n",
       "      <td>0.012607</td>\n",
       "      <td>0.055680</td>\n",
       "      <td>-0.002961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>51874</td>\n",
       "      <td>-0.064686</td>\n",
       "      <td>-0.079721</td>\n",
       "      <td>-0.052488</td>\n",
       "      <td>0.131392</td>\n",
       "      <td>-0.113462</td>\n",
       "      <td>0.006082</td>\n",
       "      <td>-0.088323</td>\n",
       "      <td>0.091039</td>\n",
       "      <td>0.179739</td>\n",
       "      <td>-0.003518</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.058225</td>\n",
       "      <td>0.090456</td>\n",
       "      <td>0.053185</td>\n",
       "      <td>-0.133539</td>\n",
       "      <td>0.178075</td>\n",
       "      <td>-0.305878</td>\n",
       "      <td>-0.060538</td>\n",
       "      <td>0.207321</td>\n",
       "      <td>0.032167</td>\n",
       "      <td>0.064919</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>51875</td>\n",
       "      <td>0.538579</td>\n",
       "      <td>0.180514</td>\n",
       "      <td>-0.126930</td>\n",
       "      <td>-0.047020</td>\n",
       "      <td>0.117155</td>\n",
       "      <td>-0.117209</td>\n",
       "      <td>0.228658</td>\n",
       "      <td>-0.068845</td>\n",
       "      <td>-0.010007</td>\n",
       "      <td>0.009729</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.011428</td>\n",
       "      <td>-0.061058</td>\n",
       "      <td>-0.031267</td>\n",
       "      <td>0.014056</td>\n",
       "      <td>-0.009959</td>\n",
       "      <td>0.004231</td>\n",
       "      <td>0.015181</td>\n",
       "      <td>-0.019029</td>\n",
       "      <td>0.048675</td>\n",
       "      <td>0.008249</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>51876</td>\n",
       "      <td>-0.465820</td>\n",
       "      <td>0.513654</td>\n",
       "      <td>-0.043011</td>\n",
       "      <td>-0.124533</td>\n",
       "      <td>0.091874</td>\n",
       "      <td>0.070319</td>\n",
       "      <td>-0.096633</td>\n",
       "      <td>-0.090940</td>\n",
       "      <td>0.124116</td>\n",
       "      <td>-0.005477</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.094153</td>\n",
       "      <td>-0.048501</td>\n",
       "      <td>0.113293</td>\n",
       "      <td>0.131095</td>\n",
       "      <td>-0.024306</td>\n",
       "      <td>-0.079771</td>\n",
       "      <td>0.088763</td>\n",
       "      <td>-0.018239</td>\n",
       "      <td>-0.073918</td>\n",
       "      <td>0.100919</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>51877</td>\n",
       "      <td>0.175080</td>\n",
       "      <td>-0.054200</td>\n",
       "      <td>0.596018</td>\n",
       "      <td>0.040989</td>\n",
       "      <td>-0.054810</td>\n",
       "      <td>0.000753</td>\n",
       "      <td>-0.130082</td>\n",
       "      <td>-0.129651</td>\n",
       "      <td>-0.205707</td>\n",
       "      <td>-0.044195</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000294</td>\n",
       "      <td>0.081824</td>\n",
       "      <td>-0.027083</td>\n",
       "      <td>0.046073</td>\n",
       "      <td>-0.001615</td>\n",
       "      <td>-0.012452</td>\n",
       "      <td>-0.026487</td>\n",
       "      <td>0.041996</td>\n",
       "      <td>0.042444</td>\n",
       "      <td>-0.032133</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>51878 rows × 48 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           pca0      pca1      pca2      pca3      pca4      pca5      pca6  \\\n",
       "0     -0.043350 -0.060584  0.272129 -0.060398 -0.078276 -0.008805 -0.001642   \n",
       "1     -0.115760 -0.197918 -0.049863  0.666364  0.668454  0.086413  0.169695   \n",
       "2      0.021702 -0.164828  0.706077  0.023347 -0.080063 -0.018041 -0.056035   \n",
       "3      0.063711  0.363892  0.147542  0.057119 -0.103717  0.282836 -0.073967   \n",
       "4     -0.081707 -0.242624  0.013579  0.528990  0.688504  0.188602  0.163560   \n",
       "...         ...       ...       ...       ...       ...       ...       ...   \n",
       "51873  0.000284 -0.068464  0.481083  0.027953 -0.031293 -0.210537  0.100820   \n",
       "51874 -0.064686 -0.079721 -0.052488  0.131392 -0.113462  0.006082 -0.088323   \n",
       "51875  0.538579  0.180514 -0.126930 -0.047020  0.117155 -0.117209  0.228658   \n",
       "51876 -0.465820  0.513654 -0.043011 -0.124533  0.091874  0.070319 -0.096633   \n",
       "51877  0.175080 -0.054200  0.596018  0.040989 -0.054810  0.000753 -0.130082   \n",
       "\n",
       "           pca7      pca8      pca9  ...     pca38     pca39     pca40  \\\n",
       "0      0.349955  0.255530 -0.122431  ...  0.085597 -0.063296  0.158532   \n",
       "1     -0.048810  0.026368 -0.005191  ... -0.008948 -0.021257 -0.033957   \n",
       "2     -0.278979 -0.177411 -0.068560  ...  0.007054 -0.014735  0.021255   \n",
       "3      0.528988  0.261934  0.136618  ...  0.162702  0.052842  0.071982   \n",
       "4      0.034647  0.015191  0.002458  ... -0.024562 -0.034539 -0.035536   \n",
       "...         ...       ...       ...  ...       ...       ...       ...   \n",
       "51873 -0.035262 -0.019532  0.049997  ... -0.020131 -0.074710  0.101019   \n",
       "51874  0.091039  0.179739 -0.003518  ... -0.058225  0.090456  0.053185   \n",
       "51875 -0.068845 -0.010007  0.009729  ... -0.011428 -0.061058 -0.031267   \n",
       "51876 -0.090940  0.124116 -0.005477  ... -0.094153 -0.048501  0.113293   \n",
       "51877 -0.129651 -0.205707 -0.044195  ... -0.000294  0.081824 -0.027083   \n",
       "\n",
       "          pca41     pca42     pca43     pca44     pca45     pca46     pca47  \n",
       "0      0.040823  0.043348  0.065940 -0.111555 -0.075880  0.000997 -0.060340  \n",
       "1     -0.000682  0.032265 -0.014482 -0.006587 -0.005308 -0.027790  0.009790  \n",
       "2      0.013600 -0.000227  0.081414  0.019109  0.026369  0.028131 -0.028596  \n",
       "3     -0.149263  0.084534  0.043217  0.091920 -0.004109 -0.041666  0.076827  \n",
       "4     -0.011611  0.032814 -0.022945  0.006963  0.003239 -0.014328 -0.005327  \n",
       "...         ...       ...       ...       ...       ...       ...       ...  \n",
       "51873 -0.043210  0.068722 -0.001956  0.050221  0.012607  0.055680 -0.002961  \n",
       "51874 -0.133539  0.178075 -0.305878 -0.060538  0.207321  0.032167  0.064919  \n",
       "51875  0.014056 -0.009959  0.004231  0.015181 -0.019029  0.048675  0.008249  \n",
       "51876  0.131095 -0.024306 -0.079771  0.088763 -0.018239 -0.073918  0.100919  \n",
       "51877  0.046073 -0.001615 -0.012452 -0.026487  0.041996  0.042444 -0.032133  \n",
       "\n",
       "[51878 rows x 48 columns]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_pca"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "test score: 0.7830679671537067 \n"
     ]
    }
   ],
   "source": [
    "print('test score: {} '.format(svc.score(x_pca, y)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "这个结果为0.78比原始结果还稍微好些"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
